<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
</head>

<body>
    <script>
        /*
            需求:   回家给女朋友做饭
            1.  去菜场买菜
            2.  回家洗菜
            3.  开始做饭
            4.  做饭完成开始吃饭
            5.  吃完饭洗碗
        */
        let flag = 1;

        function step1(resolve, reject) {
            console.log('准备去买菜');
            if (flag) {
                resolve('买完菜')
            } else {
                reject('没有菜')
            }
        }

        function step2(resolve, reject) {
            console.log('准备洗菜');
            if(flag == 1) {
                resolve('洗完菜')
            } else {
                reject('没洗成')
                flag = 0
            }
        }

        function step3(resolve, reject) {
            console.log('准备做饭');
            if(flag == 1) {
                resolve('做完饭')
            } else {
                reject('没做成')
            }
        }

        function step4(resolve, reject) {
            console.log('准备吃饭');
            if(flag == 1) {
                resolve('吃完饭')
            } else {
                reject('没吃')
            }
        }

        // new Promise(step1).then( (val1) => {
        //     console.log(val1);
        //     return new Promise(step2)
        // }).then( (val2) => {
        //     console.log(val2);
        //     return new Promise(step3)
        // }).then( val3 => {
        //     console.log(val3);
        //     return new Promise(step4)
        // }).then( val4 => {
        //     console.log(val4);
        // })



        new Promise(step1).then(val1 => {
            console.log(step1);
            return new Promise(step2)
        }).then(val2 => {
            console.log(step2);
            return new Promise(step3)
        }).then(val3 => {
            console.log(step3);
            return new Promise(step4)
        }).then(val4 => {
            console.log(step4);
            
        })


        
    </script>
</body>

</html>